/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC10/11xx Series MCU UART (8-bit/9-bit)Demo ----------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the  */
/* article, please specify in which data and procedures from STC    */
/*------------------------------------------------------------------*/

#include "STC11.h"
#include "intrins.h"
#include "string.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;


/*Define UART parity mode*/
#define NONE_PARITY     0   //None parity
#define ODD_PARITY      1   //Odd parity
#define EVEN_PARITY     2   //Even parity
#define MARK_PARITY     3   //Mark parity
#define SPACE_PARITY    4   //Space parity

#define PARITYBIT NONE_PARITY   //Testing even parity

#define UART_P1 0x80        //(AUXR1.7) switch RXD/TXD from P3.0/P3.1 to P1.6/P1.7


sbit bit9 = P2^2;           //P2.2 show UART data bit9
bit busy;
bit connected_flag = 0;
bit recv_flag_global = 0;
bit overflow_flag = 0;
char recv_buf[30] = {0};
char index = 0;
char index_tmp = 0;
void SendData(BYTE dat);
void SendString(char *s);
void SendWifi(char *dat);
void SendBuffer(char *s,char len);

code char AT_SetUp[30] =  "ATE0\r\n";
code char AT_Connect[] = "AT+CIPSTART=\"TCP\",\"192.168.2.108\",8080\r\n";
code char AT_SendData[] = "AT+CIPSEND=5\r\n";

char gSendbuf[14];
void P1_Uart(){
		AUXR1 &= 0x7f;
		AUXR1 |= UART_P1;
}
void P3_Uart(){
		AUXR1 &= 0x7f;
}
void Delay3020ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 127;
	j = 233;
	k = 148;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}
void Delay500ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 22;
	j = 3;
	k = 227;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void Delay1020us()		//@11.0592MHz
{
	unsigned char i, j;

	i = 11;
	j = 246;
	do
	{
		while (--j);
	} while (--i);
}																																	 
void Uart_Init(){
		PCON &= 0x7F;		//??????
		SCON = 0x50;		//8???,?????
		AUXR |= 0x40;		//???1???Fosc,?1T
		AUXR &= 0xFE;		//??1?????1???????
		TMOD &= 0x0F;		//?????1???
		TMOD |= 0x20;		//?????1?8???????
		TL1 = 0xFD;		//??????
		TH1 = 0xFD;		//????????
		ET1 = 0;		//?????1??
		TR1 = 1;		//?????1
}

void callbackUart(){
			if (recv_flag_global == 1 || overflow_flag == 1){
				P3_Uart();
				if(overflow_flag == 1) 
					recv_buf[29] = 0x49;
				SendBuffer(recv_buf,30);
				memset(recv_buf,0,30);
				recv_flag_global = 0;
				overflow_flag = 0;
				P1_Uart();
			}
}
void main()
{	
	char i = 0;
	bit flag1 = 0;
	bit flag2 = 0;
	 Delay3020ms();

	 Delay3020ms();
	 Delay3020ms();
		Uart_Init();

	  P1M1 = 0x00;  
		P1M0 = 0x3f;  // realy gpio init
	
		// open uart interrupt
		ES = 1;
		EA = 1;
		REN = 1;
		P1_Uart();
		Delay1020us();
		// close P1 input
		//P1 = 0X00;   
		Delay1020us();

	for( i = 0;i < 13;i++){
	   gSendbuf[i] = i+1;
	}
    while(1){
			if (flag1 == 0){
				flag1 = 1;
				SendString(AT_SetUp);
				Delay3020ms();
				
			}
			callbackUart();
			if ((flag1 == 1) && (flag2 == 0)) {
				flag2 = 1;
				SendString(AT_Connect);
				Delay3020ms();
				connected_flag = 1;
				//SendWifi("12345");
			}
			callbackUart();
		};
}
/*
 0D 0A 4F 4B 0D 0A   \r\nOK\r\n
 43 4F 4E 4E 45 43 54 0D 0A 0D 0A 4F 4B 0D 0A 					CONNECT\r\nOK
 +IPD,5:12345		  
*/
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4
{
	static char last_byte;

	static bit flag_recv = 0;
	static bit flag_need_read = 0;
	static char recv_cnt = 0;
	 
    if (RI)
    {
        RI = 0;             //Clear receive interrupt flag
		if((last_byte == '\r') 
		&&(SBUF == '\n' )
		&& (index != 1)
		&&(connected_flag == 0)){
			recv_buf[index + 1] = '\0';
			recv_flag_global = 1;
			index = 0;
			last_byte = 0;

			//todo 	 
		}
		if(
			(recv_buf[index - 1] == ':') 
			&&(recv_buf[index - 2] == '5' ) 
			&&(recv_buf[index - 3] == ',' )
			&&(recv_buf[index - 4] == 'D')
			&&index > 4)
		{
		   flag_need_read = 1;
		   recv_cnt = 4;
		}
		if(flag_need_read == 1){
			if(recv_cnt > 0){
				recv_cnt --;			
			}else{
				recv_buf[index + 1] = "\0";
				recv_flag_global = 1;
				index = 0;
				recv_cnt = 0;
				flag_need_read = 0;
			}
		}      
        recv_buf[index] = SBUF;         
		last_byte = SBUF;
		index ++;
        if(index > 29){
			index = 0;
			overflow_flag = 1;
		}
    }
    if (TI)
    {
        TI = 0;             //Clear transmit interrupt flag
        busy = 0;           //Clear transmit busy flag
    }
}

/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
	while(busy == 1) return;
    ACC = dat;              //Calculate the even parity bit P (PSW.0)

    busy = 1;
    SBUF = ACC;             //Send data to UART buffer
		while(busy == 1) ;
}

/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
    while (*s != '\0')              //Check the end of the string
    {
		Delay1020us();
        SendData(*s++);     //Send current char and increment string ptr
    }
}
void SendWifi(char *dat){
	  SendString(AT_SendData);
	  Delay3020ms();
	  SendString(dat);
}
void SendBuffer(char *s,char len){

    while (len > 0)              //Check the end of the string
    {
		Delay1020us();
        SendData(*s++);     //Send current char and increment string ptr
		len --;
    }
}